﻿using System;
using System.Data;
using System.Web;
using System.Text;
using System.IO;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.BIZ;
using System.Reflection;
using System.Data.SqlClient;
using log4net;
using Microsoft.ApplicationBlocks.Data;


namespace AnyOffice.general.system.res_manage
{
    public partial class user_res : ModulePage
    {

        protected long folderNums;
        protected long folderSize;
        protected long inboxNums;
        protected long inboxSize;
        protected long outboxNums;
        protected long outboxSize;
        protected long totalSize;

        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        
        private void GetDirInfo(DirectoryInfo d)
        {
            foreach (FileInfo info in d.GetFiles())
            {
                this.folderSize += info.Length;
                this.folderNums += 1L;
            }
            foreach (DirectoryInfo info2 in d.GetDirectories())
            {
                this.GetDirInfo(info2);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string userId = (Request.QueryString["user_id"] != null) ? Request.QueryString["user_id"].ToString() : "";
            string str2 = (Request.QueryString["user_name"] != null) ? Request.QueryString["user_name"].ToString() : "";
            if (userId != string.Empty)
            {
                try
                {
                    SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@UserId", SqlDbType.NVarChar, 20) };
                    commandParameters[0].Value = userId;
                    string commandText = "SELECT COUNT([EmailId]) AS InboxNums,SUM([TotalSize]) AS InboxSize FROM [dbo].[Email] WHERE [ToId]=@UserId AND [DeleteFlag] IN (0,2)";
                    DataTable table = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , commandText, commandParameters).Tables[0];
                    if (table.Rows.Count > 0)
                    {
                        this.inboxNums = Convert.ToInt64(table.Rows[0]["InboxNums"].ToString());
                        if (table.Rows[0]["InboxSize"] != DBNull.Value)
                        {
                            this.inboxSize = Convert.ToInt64(table.Rows[0]["InboxSize"].ToString());
                        }
                    }
                    commandText = "SELECT COUNT([EmailId]) AS OutboxNums,SUM([TotalSize]) AS OutboxSize FROM [dbo].[Email] WHERE [FromId]=@UserId AND [DeleteFlag] IN (0,1)";
                    DataTable table2 = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText, commandParameters).Tables[0];
                    if (table2.Rows.Count > 0)
                    {
                        this.outboxNums = Convert.ToInt64(table2.Rows[0]["OutboxNums"].ToString());
                        if (table2.Rows[0]["OutboxSize"] != DBNull.Value)
                        {
                            this.outboxSize = Convert.ToInt64(table2.Rows[0]["OutboxSize"].ToString());
                        }
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }
                try
                {
                    string path = HttpContext.Current.Server.MapPath("/attachment/file_folder/" + userId);
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    this.GetDirInfo(new DirectoryInfo(path));
                }
                catch (Exception exception2)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception2);
                    }
                }
                UserCache cache = new UserCache(userId);
                if (cache.EmailCapacity == 0)
                {
                    this.litEmailCapacity.Text = "无限制";
                }
                else
                {
                    this.litEmailCapacity.Text = cache.EmailCapacity.ToString() + "M";
                }
                if (cache.FolderCapacity == 0)
                {
                    this.litFolderCapacity.Text = "无限制";
                }
                else
                {
                    this.litFolderCapacity.Text = cache.FolderCapacity.ToString() + "M";
                }
                this.litInboxNums.Text = this.inboxNums.ToString();
                this.litInboxSize.Text = this.inboxSize.ToString("n0") + "字节 （约合" + StringHelper.FormatSize(this.inboxSize) + "）";
                this.litOutboxNums.Text = this.outboxNums.ToString();
                this.litOutboxSize.Text = this.outboxSize.ToString("n0") + "字节 （约合" + StringHelper.FormatSize(this.outboxSize) + "）";
                this.litTotalNums.Text = Convert.ToString((long)(this.inboxNums + this.outboxNums));
                this.totalSize = this.inboxSize + this.outboxSize;
                this.litTotalSize.Text = this.totalSize.ToString("n0") + "字节 （约合" + StringHelper.FormatSize(this.totalSize) + "）";
                this.litFolderNums.Text = this.folderNums.ToString();
                this.litFolderSize.Text = this.folderSize.ToString("n0") + "字节 （约合" + StringHelper.FormatSize(this.folderSize) + "）";
                this.litUser.Text = base.ShowPromptNoNav("用户：" + str2 + "<br>总合计：" + StringHelper.FormatSize((this.inboxSize + this.outboxSize) + this.folderSize));
            }
        }
 

    }
}
